import 'package:dio/dio.dart';
import 'package:myapp/http/http_method.dart';
import 'package:myapp/http/interceptor/cookie_intercepter.dart';
import 'package:myapp/http/interceptor/print_log_interceptor.dart';
import 'package:myapp/http/interceptor/resp_interceptor.dart';

class DioInstance {
  static DioInstance? _instance;

  DioInstance._internal();

  static DioInstance instance() {
    return _instance ??= DioInstance._internal();
  }

  final Dio _dio = Dio();
  final _defaultTimeout = const Duration(seconds: 30);
  var _inited = false;

  void initDio({
    required String baseUrl,
    String? method = HttpMethod.GET,
    Duration? connectTimeout,
    Duration? receiveTimeout,
    Duration? sendTimeout,
    ResponseType? responseType = ResponseType.json,
    String? contentType,
  }) {
    _dio.options = buildBaseOptions(
      baseUrl: baseUrl,
      method: method,
      connectTimeout: connectTimeout ?? _defaultTimeout,
      receiveTimeout: receiveTimeout ?? _defaultTimeout,
      sendTimeout: sendTimeout ?? _defaultTimeout,
      responseType: responseType,
      contentType: contentType,
    );
    _inited = true;
    ///添加拦截器
    _dio.interceptors.add(PrintLogInterceptor());
    ///添加 ResponseInterceptor
    _dio.interceptors.add(ResponseInterceptor());
    ///添加 CookieInterceptor
    _dio.interceptors.add(CookieInterceptor());
  }

  BaseOptions buildBaseOptions({
    required String baseUrl,
    String? method,
    Duration? connectTimeout,
    Duration? receiveTimeout,
    Duration? sendTimeout,
    ResponseType? responseType = ResponseType.json,
    String? contentType,
  }) {
    return BaseOptions(
      method: method,
      baseUrl: baseUrl,
      connectTimeout: connectTimeout ?? _defaultTimeout,
      receiveTimeout: connectTimeout ?? _defaultTimeout,
      sendTimeout: connectTimeout ?? _defaultTimeout,
      responseType: responseType,
      contentType: contentType,
    );
  }

  //get请求方式
  Future<Response> get({
    required String path,
    Map<String, dynamic>? param,
    Options? options,
    CancelToken? cancelToken,}) async {
    if (!_inited) {
      throw Exception("you shoud  call initDio() first!");
    }
    return _dio.get(
      path,
      queryParameters: param,
      options: options ??
          Options(
            method: HttpMethod.GET,
            receiveTimeout: _defaultTimeout,
            sendTimeout: _defaultTimeout,
          ),
      cancelToken: cancelToken,);
  }

  void changeBaseUrl(String baseUrl){
    _dio.options.baseUrl = baseUrl;
  }
  //post请求方式
  Future<Response> post ({
    required String path,
    Object? data,
    Map<String, dynamic>? queryParameters,
    Options? options,
    CancelToken? cancelToken,}) async {
    if (!_inited) {
      throw Exception("you should  call initDio() first!");
    }
    return _dio.post(
      path,
      data: data,
      queryParameters: queryParameters,
      options: options ??
          Options(
            method: HttpMethod.GET,
            receiveTimeout: _defaultTimeout,
            sendTimeout: _defaultTimeout,
          ),
      cancelToken: cancelToken,
    );
  }
}
